package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsStorageMapper;
import com.qf.pojo.DtsStorage;
import com.qf.service.StorageService;
import com.qf.util.AliyunOSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.UUID;

@Service
public class StorageServiceImpl implements StorageService {

    @Autowired
    private DtsStorageMapper storageMapper ;

    @Value("${aliyun.oss.endpoint}")
    private String endpoint;

    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;

    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;

    @Value("${aliyun.oss.bucketName}")
    private String bucketName;


    @Override
    public IPage<DtsStorage> findByPage(Integer page, Integer limit, String key, String name, String sort, String order) {
        //分页对象                第一参数：当前页 第二参数：每页显示条数
        Page<DtsStorage> iPage = new Page<>(page,limit);

        QueryWrapper queryWrapper = new QueryWrapper();
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.eq("key", key);
        }
        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("name", name);
        }


        if (order.equalsIgnoreCase("asc")){
            queryWrapper.orderByAsc(sort);
        }else {
            queryWrapper.orderByDesc(sort);
        }

        Page<DtsStorage> resultPage = storageMapper.selectPage(iPage, queryWrapper);
        return resultPage;
    }

    @Override
    public DtsStorage add(String name, String ext, Integer size, byte[] content) throws Exception {
        //1.将文件上传到阿里云OSS存储
        //使用随机字符串生成文件的key
        String key = UUID.randomUUID().toString();

        //将文件存储到阿里云当中
        String url = AliyunOSSUtil.uploadImage(endpoint, accessKeyId, accessKeySecret, bucketName, key + ext, content);


        //2.将文件保存路径存储到mysql数据库的storage表中
        DtsStorage storage = new DtsStorage();
        //文件上传路径
        storage.setUrl(url);
        //更新时间
        storage.setUpdateTime(new Date());
        //添加时间
        storage.setAddTime(new Date());
        //文件大小
        storage.setSize(size);
        //文件类型
        storage.setType(ext);
        //文件名
        storage.setName(name);
        //文件唯一标识
        storage.setKey(key+ext);
        storage.setDeleted(false);

        storageMapper.insert(storage);

        return storage;
    }

    @Override
    public int update(DtsStorage storageInfo) {
        storageInfo.setUpdateTime(new Date());
        int update = storageMapper.updateById(storageInfo);
        return update;
    }

    @Override
    public void deleteByKey(DtsStorage dtsStorage) {
        QueryWrapper<DtsStorage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", dtsStorage.getId());

        AliyunOSSUtil.deleteFile(endpoint, accessKeyId, accessKeySecret, bucketName, dtsStorage.getKey());

        storageMapper.delete(queryWrapper);
    }
}
